//
//  BasicService.m
//  fmdb_demo
//
//  Created by rujia chen on 15/10/18.
//  Copyright (c) 2015年 dfrz. All rights reserved.
//

#import "BasicService.h"
#import <AFNetworking/AFNetworking.h>

@implementation BasicService

+ (void)GET:(NSString *)url params:(NSString *)params success:(ResponseBlock)success fail:(ResponseBlock)fail networkFail:(NetworkFailBlock)networkFail{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json",@"text/html",@"text/json",@"text/javascription",@"application/x-javascript", nil]];
    
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        ResponseModel *responseModel = [[ResponseModel alloc]initWithDictionary:responseObject];
        if (responseModel.resultcode == 0) {
            if (success) {
                success(responseModel);
            }
        }else{
            if (fail) {
                fail(responseModel);
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError * error) {
        if (networkFail) {
            networkFail(error);
        }
    }];
    
}

+ (void)GETForText:(NSString *)url params:(NSString *)params success:(ResponseDataBlock)success networkFail:(NetworkFailBlock)networkFail{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json",@"text/html",@"text/json",@"text/javascription",@"application/x-javascript", nil]];
    
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError * error) {
        if (networkFail) {
            networkFail(error);
        }
    }];
    
}


@end
